package com.bsh.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsh.api.params.DtoRegion;
import com.bsh.mybatis.model.Region;
import com.bsh.service.AccountService;
import com.bsh.service.MerchantService;
import com.bsh.service.RegionService;
import com.bsh.service.SmsService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Des3;

/**
 * 商户相关 api
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/region", method = { RequestMethod.POST })
public class ApiRegionController {
	private static Logger log = LoggerFactory.getLogger(ApiRegionController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	UserService servUser;
	@Autowired
	SmsService servSms;
	@Autowired
	AccountService accountServ;
	@Autowired
	private MerchantService mercService;
	@Autowired
	RegionService regionServ;
	
 
		
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiMessage regionList(@RequestParam(value="paramStr", required=true) String paramStr) {
		ApiMessage msg = null;
		DtoRegion dto=Des3.decRequest(paramStr, DtoRegion.class);

//		if (StringUtils.isNullOrEmpty(dto.getToken())) {
//			msg = new ApiMessage("103", "缺少token");
//			return msg;
//		}
//		boolean checkToken = ApiUtils.checkToken( dto.getUserName(), dto.getToken() );
//		if( checkToken == false ){
//			msg = new ApiMessage("400", "token错误");
//			return msg;
//		}
		msg = new ApiMessage("200", "地域列表");
		//pid =1所有省
		int pid = dto.getPid();
		List<Region> res = regionServ.getRegionList(pid);
	
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("token", dto.getToken() );
		data.put("info", res);
		msg.setData(Des3.encResponse(data));
		
		return msg;
	}

	@RequestMapping(value = "/allcity", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage regionAllCity() {
		ApiMessage msg = null;
//		DtoRegion dto=Des3.decRequest(paramStr, DtoRegion.class);

//		if (StringUtils.isNullOrEmpty(dto.getToken())) {
//			msg = new ApiMessage("103", "缺少token");
//			return msg;
//		}
//		boolean checkToken = ApiUtils.checkToken( dto.getUserName(), dto.getToken() );
//		if( checkToken == false ){
//			msg = new ApiMessage("400", "token错误");
//			return msg;
//		}
		msg = new ApiMessage("200", "所有城市列表");
		//region_level =2 所有城市
		Integer regionLevel = 2;
		List<Region> res = regionServ.getRegionAllCity(regionLevel);
	
		Map<String, Object> data = new HashMap<String, Object>();
//		data.put("token", dto.getToken() );
		data.put("info", res);
		msg.setData(Des3.encResponse(data));
		
		return msg;
	}
	
	
	@RequestMapping(value = "/merchantCity", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage merchantCity() {
		ApiMessage msg = null;
//		DtoRegion dto=Des3.decRequest(paramStr, DtoRegion.class);

//		if (StringUtils.isNullOrEmpty(dto.getToken())) {
//			msg = new ApiMessage("103", "缺少token");
//			return msg;
//		}
		msg = new ApiMessage("200", "商户的城市列表");
		String where="where region_level=2 and id in (SELECT city from merchant) ";
		List<Map<String, Object>>  res = regionServ.getSelectedRegion(where);
		Map<String, Object> data = new HashMap<String, Object>();
//		data.put("token", dto.getToken() );
		data.put("info", res);
		msg.setData(Des3.encResponse(data));
		return msg;
	}

}
